<?php 
require '../../frame/base.php';
define('KEYS','$10@w/2e3U4H');

$set = !empty($_REQUEST['set']) ? json_decode($_REQUEST['set']) : null;
$uid = !empty($_REQUEST['uid']) ? $_REQUEST['uid'] : null;
$sid = !empty($_REQUEST['sid']) ? $_REQUEST['sid'] : null;
// $weiname = !empty($_REQUEST['weiname']) ? $_REQUEST['weiname'] : '';
// $alipayname = !empty($_REQUEST['alipayname']) ? $_REQUEST['alipayname'] : '';
// $realname = !empty($_REQUEST['realname']) ? $_REQUEST['realname'] : '';
// $telphone = !empty($_REQUEST['telphone']) ? $_REQUEST['telphone'] : '';

// var_dump($set->passwd);die;
if($set){sleep(1);

	if($uid){
		$passwd = KEYS.$set->passwd;
		// var_dump($passwd);die;
		// $passwd = $set->passwd;
		// var_dump($passwd);die;
		if( $data=$Db->table('user')->where('userid',$uid)->where('passwd',$passwd)->find() ){
				// echo json_encode(['code'=>200,'msg'=>'验证成功','data'=>$data['userid']]);
				$money=$data['money']-(float)$_REQUEST['money'];

				switch ($_REQUEST['method'])
				{
					case '1':
					  $payInfo = $Db->table('blind_wei')->where('id',$_REQUEST['payId'])->find();
					  break;
					case '2':
					  $payInfo = $Db->table('blind_alipay')->where('id',$_REQUEST['payId'])->find();
					  break;
					case '3':
					  $payInfo = $Db->table('blind_banks')->where('id',$_REQUEST['payId'])->find();
					  break;
					default:
					  echo "No number between 1 and 3";
				}

				if(isset($payInfo['weipay']) == true) {
					$payInfo['account'] = $payInfo['weipay'];
				} elseif(isset($payInfo['ailpay']) == true) {
					$payInfo['account'] = $payInfo['ailpay'];
				} elseif(isset($payInfo['bank_account']) == true) {
					$payInfo['account'] = $payInfo['bank_account'];
				}
				
				if($money>=0){
					
					$restId = $Db->table('present')->insertGetId([
						'account_money'=>$_REQUEST['money'],
						'account'=>$payInfo['account'],
						'uid'=>$uid,
						'account_method'=>$_REQUEST['method'],
						'account_bank'=>isset($payInfo['bid'])?$payInfo['bid']:NULL,
						'weiname'=>isset($payInfo['weixin_name'])?$payInfo['weixin_name']:NULL,
						'alipayname'=>isset($payInfo['alipay_name'])?$payInfo['alipay_name']:NULL,
						'realname'=>isset($payInfo['realname'])?$payInfo['realname']:NULL,
						'telphone'=>isset($payInfo['telphone'])?$payInfo['telphone']:NULL,
					]);

					$Db->table('user')->where('userid',$uid)->update([
						'money'=>$money,
					]);

				echo json_encode(['code'=>200,'msg'=>'提现成功','data'=>$data['userid']]);
				}else{
					echo json_encode(['code'=>200,'msg'=>'余额不足']);
				}

		}else{
			echo json_encode(['code'=>100,'msg'=>'密码错误','data'=>$data['userid']]);
		}
	}elseif($sid){

		$passwd=KEYS.$set->passwd;
		// $passwd = $set->passwd;
		
		if( $data = $Db->table('shop')->where('shopid',$sid)->where('passwd',$passwd)->find() ){
				echo json_encode(['code'=>200,'msg'=>'验证成功','data'=>$data['shopid']]);
				$money=$data['money']-(float)$_REQUEST['money'];
				switch ($_REQUEST['method'])
				{
					case '1':
					  $payInfo = $Db->table('blind_wei')->where('id',$_REQUEST['payId'])->find();
					  break;
					case '2':
					  $payInfo = $Db->table('blind_alipay')->where('id',$_REQUEST['payId'])->find();
					  break;
					case '3':
					  $payInfo = $Db->table('blind_banks')->where('id',$_REQUEST['payId'])->find();
					  break;
					default:
					  echo "No number between 1 and 3";
				}

				if(isset($payInfo['weipay']) == true) {
					$payInfo['account'] = $payInfo['weipay'];
				} elseif(isset($payInfo['ailpay']) == true) {
					$payInfo['account'] = $payInfo['ailpay'];
				} elseif(isset($payInfo['bank_account']) == true) {
					$payInfo['account'] = $payInfo['bank_account'];
				}

				
				if($money>=0){
					$restId = $Db->table('present')->insertGetId([
						'sid'=>$sid,
						'account'=>$payInfo['bank_account'],
						'account_money'=>$_REQUEST['money'],
						'account_method'=>$_REQUEST['method'],
						'account_bank'=>isset($payInfo['bid'])?$payInfo['bid']:NULL,
						'weiname'=>isset($payInfo['weixin_name'])?$payInfo['weixin_name']:NULL,
						'alipayname'=>isset($payInfo['alipay_name'])?$payInfo['alipay_name']:NULL,
						'realname'=>isset($payInfo['realname'])?$payInfo['realname']:NULL,
						'telphone'=>isset($payInfo['telphone'])?$payInfo['telphone']:NULL,
						
					]);
				$Db->table('shop')->where('shopid',$sid)->update([
					'money'=>$money,
				]);
				}else{
					echo json_encode(['code'=>200,'msg'=>'余额不足']);
				}

		}else{
			echo json_encode(['code'=>100,'msg'=>'密码错误','data'=>$data['userid']]);	
		}
	}

}



// 加密
function encrypt($str, $key = '$1@9')
{
    $coded = '';
    $keylength = strlen($key);

    for ($i = 0, $count = strlen($str); $i < $count; $i += $keylength)
    {
        $coded .= substr($str, $i, $keylength) ^ $key;
    }

    return str_replace('=', '', base64_encode($coded));
}


// 解密
function decrypt($str, $key = '$1@9')
{
    $coded = '';
    $keylength = strlen($key);
    $str = base64_decode($str);

    for ($i = 0, $count = strlen($str); $i < $count; $i += $keylength)
    {
        $coded .= substr($str, $i, $keylength) ^ $key;
    }

    return $coded;
}